/*
 * @Description: 抽取单表增查删改
 * @Date: 2019-10-14 11:08:46
 * @LastEditTime: 2020-03-19 12:26:38
 * @LastEditors: 魏勇
 */

import {_initColWidth, _changeDbRecordToJson,changeDbFieldToJson,_changToAddDomain, _getFieldFromDb,extendFields} from '../data/domainCols.js';
import cols from '../data/domainCols.js';
import { Loading } from 'element-ui';

let loadingInstance=null;
export default {
    components: {},
    data() {
        return {
            tableHeight:0,
            operations:[],
            isShowPurchaseRecordsWin: false,
            isShowPurchaseWin: false,
            maxHeight: parseFloat(window.innerHeight) - 235,
            extendBtns: [
                {id: 'purchase', label: '购买', icon: 'el-icon-sold-out'},
                {id: 'purchaseRecords', label: '购买记录', icon: 'el-icon-tickets'}
            ],
            winWidth: (parseFloat(900)/parseFloat(document.body.clientWidth))*100+"%",
            //列集合
            gridCols: [],
            formCols:[],
        }
    },
    //渲染之前 动态加载列表 和 form的相关动态列数据
    beforeMount() {
         loadingInstance = Loading.service({text:"加载中"});
        let formCols = [];let gridCols=[];
        cols&&cols.forEach((itemObj) => {
            formCols.push({...itemObj});
        });
        cols&&cols.forEach((itemObj) => {
            gridCols.push({...itemObj});
        });
        this.getDbField(formCols,gridCols);
    },
    mounted(){
        this.$nextTick(()=>{
            this.tableHeight=parseFloat(window.innerHeight) - 235
        })
    },
    computed: {},
    methods: {
        searchGridByResName(resourceName){
            this.$refs.tableObj.initPagin();
            this.$refs.tableObj.flushGridByUrl("findResourceByCondition", {
                method: "POST", customRequest: true, dataFun: (list) => {
                    let recordJsonAry = [];
                    list.forEach((itemObj, index) => {
                        recordJsonAry.push(_changeDbRecordToJson(itemObj["commonTenantResource"]));
                    });
                    return recordJsonAry;
                }
            },{resourceName,resourceType:this.tabType});
        },
        searchGridByCondition(resourceName,backFun){
            this.$refs.tableObj.initPagin();
            this.$refs.tableObj.flushGridByUrl("findCommonTenantResources", {
                method: "get", customRequest: true, dataFun: (list) => {
                    let recordJsonAry = [];
                    list.forEach((itemObj, index) => {
                        recordJsonAry.push(_changeDbRecordToJson(itemObj["commonTenantResource"]));
                    });
                    backFun&&backFun();
                    return recordJsonAry;
                }
            },{resourceName});
        },
        //根据资源类型动态获取列数据信息
        getDbField(formCols,gridCols) {
            this.api.getDbField({params: {type: this.tabType}}, {method: "get", customRequest: true})
                .then(res => {
                    _getFieldFromDb(res["data"], formCols,gridCols);
                    if(formCols!=null){
                        this.formCols=formCols;
                    }
                    if(gridCols!=null){
                        gridCols=gridCols.concat(extendFields);
                        this.gridCols = gridCols;
                    }
                    loadingInstance.close();
                })
                .catch(err => {
                    console.warn(err.msg)
                });
        },
        //修改前加载数据
        loadRecord(id) {
            this.api.getCommonTenantResourceById({params: {id}}, {method: "get", customRequest: true})
                .then(res => {
                    changeDbFieldToJson.call(this, res);
                })
                .catch(err => {
                    console.warn(err.msg)
                });
        },
        //增加前的方法
        beforeAdd() {
            this.curRecordId = null;
            // _initColWidth(cols);
            this.formCols.forEach((itemObj) => {
                itemObj["value"] = "";
                    itemObj.subItems&&itemObj.subItems.forEach((subItemObj)=>{
                        subItemObj["value"] = "";
                    });
                    if(itemObj.itemDatas!=null){
                        itemObj.itemDatas=[itemObj.subItems];
                    }
            })
        },
        //添加操作
        confirmOk() {
            if (this.curRecordId != null && this.curRecordId !== "") {
                this.modRecordToDb();
            } else {
                this.addRecordToDb();
            }
        },
        //添加数据到数据库
        addRecordToDb() {
            let params = _changToAddDomain(this.formCols, this.tabType);
            if(this.isShowPurchaseWin){
                params["createType"]=1;
            }else{
                params["createType"]=0;
            }
            this.api.addResource(params)
                .then(res => {
                    this.isShowAddDialog = false;
                    this.isShowPurchaseWin=false;
                    this.curRecordId = null;
                    this.$refs.tableObj.reloadData();
                })
                .catch(err => {
                    this.isShowAddDialog = false;
                    this.isShowPurchaseWin=false;
                    this.$refs.tableObj.reloadData();
                });
        },
        //修改数据到数据库
        modRecordToDb() {
            let params = _changToAddDomain(this.formCols, this.tabType);
            params["id"] = this.curRecordId;
            this.api.updateResource(params)
                .then(res => {
                    this.isShowAddDialog = false;
                    this.curRecordId = null;
                    this.$refs.tableObj.reloadData();
                })
                .catch(err => {
                    console.warn(err.msg)
                });
        },
        //删除操作
        delOk(ids) {
            var params={ids:[ids]};
            this.api.deleteResourceById(params)
                .then(res => {
                    this.$refs.tableObj.reloadData();
                })
                .catch(err => {
                    console.warn(err.msg)
                });
        }, //关闭schema操作窗口
        closeWin(){

        },
        //操作栏 按钮点击事件
        operationClick({row}){

        }
    }

}